#ifndef _VULKAN_CAMERA_H_
#define _VULKAN_CAMERA_H_

namespace Vulkan {
	
	class Camera
	{
	public:
		float zoom = -3.75f;
		glm::vec3 rotation = glm::vec3();
		glm::vec3 position = glm::vec3();

		struct{
			glm::mat4 project;
			glm::mat4 model = glm::mat4(1.0f);
			glm::mat4 view;
		}mvp;
		VkDescriptorBufferInfo descriptor;

		struct
		{
			VkBuffer buffer = VK_NULL_HANDLE;
			VkDeviceMemory memory = VK_NULL_HANDLE;
		}mvpBuffer,stagingMvpBuffer;

		void init() {
			setupBuffer();
			descriptor = { mvpBuffer.buffer ,0,sizeof(mvp) };
		}

		void setPerspective(float fov, float aspect, float znear, float zfar)
		{
			mvp.project = glm::perspective(glm::radians(fov), aspect, znear, zfar);
		};

		inline void update() {
			updateViewMatrix();
			auto size = sizeof(mvp);
			mapData(stagingMvpBuffer.memory, size, &mvp);

			VkBufferCopy copyRegion = {};
			copyRegion.size = size;
			auto cmd = beginSingleCommandBuffer();
			vkCmdCopyBuffer(cmd, stagingMvpBuffer.buffer, mvpBuffer.buffer, 1, &copyRegion);
			flushSingleCommandBuffer(cmd);
		}

	private:
		inline void setupBuffer() {
			VkDeviceSize size = sizeof(mvp);

			stagingMvpBuffer.buffer = createBuffer(size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
			stagingMvpBuffer.memory = createMemory(stagingMvpBuffer.buffer, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);

			mvpBuffer.buffer = createBuffer(size, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
			mvpBuffer.memory = createMemory(mvpBuffer.buffer, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
		}

		void updateViewMatrix()
		{
			mvp.view = glm::lookAt(glm::vec3(0, 0, -zoom),position,glm::vec3(0, 1, 0));

			mvp.model = glm::mat4();
			mvp.model = glm::rotate(mvp.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
			mvp.model = glm::rotate(mvp.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
			mvp.model = glm::rotate(mvp.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));

			//mapData(mvpBuffer.memory, sizeof(mvp), &mvp);
		};
	};
}

#endif
